package com.lgy.bean;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;

public class Result<T> {
	public final static String SUCCESS = "success";
	public final static String FAILED = "failed";

	private String status = SUCCESS;
	private String code = ResultCode.CODE_SUCCESS;
	private T t;
	private String msg;

	public Result() {
	}

	public Result(T t) {
		this.t = t;
	}

	public Result(String status, String msg) {
		this.status = status;
		this.msg = msg;
	}

	public Result<T> failed(String msg) {
		this.status = FAILED;
		this.code = ResultCode.CODE_FAILED;
		this.msg = ResultCode.getCodeMsg(ResultCode.CODE_FAILED) + ":" + msg;
		return this;
	}

	public Result<T> failed(String msg, String errorCode) {
		this.status = FAILED;
		this.code = errorCode;
		this.msg = ResultCode.getCodeMsg(errorCode) + ":" + msg;
		return this;
	}

	public Result<T> success(T t) {
		this.status = SUCCESS;
		this.code = ResultCode.CODE_SUCCESS;
		this.t = t;
		return this;
	}

	public Result<T> success() {
		this.status = SUCCESS;
		this.code = ResultCode.CODE_SUCCESS;
		return this;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public T getT() {
		return t;
	}

	public void setT(T t) {
		this.t = t;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@JsonIgnore
	@JSONField(serialize = false)
	public boolean isSuccessed() {
		return SUCCESS.equals(this.status);
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

}
